package org.blame.myRabbitMQ;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class ConsumerDemo {
    public static void main(String[] args) throws IOException, TimeoutException {
//        1.建立连接
        ConnectionFactory connectionFactory=new ConnectionFactory();
        connectionFactory.setHost("123.57.4.74");
        connectionFactory.setPort(5672);
        connectionFactory.setUsername("admin");
        connectionFactory.setPassword("admin");
        connectionFactory.setVirtualHost("blog");

        Connection connection= connectionFactory.newConnection();

//        2.创建channel
        Channel channel=connection.createChannel();

//        3.声明队列
//        channel.queueDeclare("666",true,false,false,null);
//        4.消费消息
        DefaultConsumer consumer=new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope,
                                       AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("接收消息"+new String(body));
            }
        };

        channel.basicConsume("666",true,consumer);
        System.out.println("消费消息成功");
//        5.释放资源
        channel.close();
        connection.close();
    }
}
